import { ref } from "vue";

// 列表数据
export const useTableList = (fetchUrl, list, callback?: any, params?: any) => {
  const total = ref(0);
  const getApiTableList = ({ currentPage, pageSize, search }) => {
    return new Promise((resolve, reject) => {
      fetchUrl({
        pageNo: currentPage,
        pageSize: pageSize,
        ...search,
        ...params,
      })
        .then((res: any) => {
          if (callback) {
            list.value = callback(res.list);
          } else {
            list.value = res.list;
          }
          total.value = res.count;
          resolve(list.value);
        })
        .catch((err) => {
          reject(new Error(err));
        });
    });
  };
  return {
    getApiTableList,
    total,
  };
};
